# Project properties
cmake_minimum_required(VERSION 3.8)

project(kvalloc)

if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Debug")
endif()

find_library(PMEM_LIBRARY NAMES pmem REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -fmax-errors=5")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fmax-errors=5 -g -O0")

include_directories(include)
link_libraries(${PMEM_LIBRARY})

set(SLAB_SRC src/kvalloc.cc src/slab.cc)
add_library(kvalloc ${SLAB_SRC})

option(KVALLOC_TEST "compile test codes" OFF)
if(KVALLOC_TEST)
  # # Download and unpack googletest at configure time
  configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
    RESULT_VARIABLE result
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
  if(result)
    message(FATAL_ERROR "CMake step for googletest failed: ${result}")
  endif()
  execute_process(COMMAND ${CMAKE_COMMAND} --build .
    RESULT_VARIABLE result
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
  if(result)
    message(FATAL_ERROR "Build step for googletest failed: ${result}")
  endif()
  add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
                  ${CMAKE_CURRENT_BINARY_DIR}/googletest-build
                  EXCLUDE_FROM_ALL)

  add_executable(single test/single.cc)
  target_link_libraries(single kvalloc gtest_main)

  add_executable(multi test/multi.cc)
  target_link_libraries(multi kvalloc gtest_main)
endif()